package com.taomu.app.zongchouhuli.moudle.mine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.framework.UsualViewOperator;
import com.taomu.app.zongchouhuli.http.res.ResWalletEntity;
import com.taomu.app.zongchouhuli.moudle.mine.activity.UserIncomeDetailActivity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.WalletListAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.WalletListContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.WalletListPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.WalletListEntity;
import com.taomu.app.zongchouhuli.utils.ListViewUtils;
import com.taomu.app.zongchouhuli.utils.TokenDialog;
import com.taomu.app.zongchouhuli.weight.EmptyRecyclerView;

import java.net.IDN;
import java.util.List;

public class WalletListFragment extends BaseFragment<WalletListContract.Presenter> implements WalletListContract.View {

    private EmptyRecyclerView mWalletRecy;

    private WalletListAdp mWalletListAdp;

    private SmartRefreshLayout mRefreshLayout;

    public static final String WALLETLISTTYPEID = "walletlisttypeid";

    public static WalletListFragment getInsence(){
        return new WalletListFragment();
    }

    private int ship = 0;

    private int take = 10;

    public static WalletListFragment getInstence(String id){
        WalletListFragment fragment = new WalletListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(WALLETLISTTYPEID,id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wallet_list;
    }

    @Override
    protected void initView(View view) {
        mRefreshLayout = view.findViewById(R.id.wallet_refresh);
        mWalletListAdp = new WalletListAdp(getContext());
        mWalletRecy = view.findViewById(R.id.wallet_list_recy);
        mWalletRecy.setLayoutManager(new LinearLayoutManager(getContext()));
        mWalletRecy.setAdapter(mWalletListAdp);
        ListViewUtils.setDefaultEmpty(mWalletRecy);
    }

    private String typid = "-1";

    @Override
    protected void initData() {

        Log.d("dwqdwqdwqdq","initData");
    }

    @Override
    protected void initEvent() {
        mWalletListAdp.setWalletListAdpListener(new WalletListAdp.WalletListAdpListener() {
            @Override
            public void onItemClick(ResWalletEntity.ListBean entity) {
                if("2".equals(typid)) {
                    UserIncomeDetailActivity.start(getContext(), String.valueOf(entity.getId()));
                }
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getWalletListData(typid,ship,take);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.getWalletListData(typid,ship,take);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });
    }

    @Override
    protected WalletListContract.Presenter createPresenter() {
        return new WalletListPresenter(this);
    }

    private boolean isFirstIn = true;

    //懒加载数据
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isFirstIn) {
            try {
                //todo:请求数据
                isFirstIn = false;
                Log.d("dwqdwqdwqdq","setUserVisibleHint");
                loadData();
            } catch (Exception e) {

            }
        }
    }

    private Handler handler = new Handler();

    @Override
    public void setWalletListData(BaseChangeResponse<ResWalletEntity> mData) {
        //mWalletListAdp.setData(mData);
        if(mRefreshLayout.isLoading()){
            mWalletListAdp.addData(mData.getData().getList());
        }else if(mRefreshLayout.isRefreshing()){
            mWalletListAdp.setData(mData.getData().getList());
        }else{
            mWalletListAdp.setData(mData.getData().getList());
        }
        ship = mWalletListAdp.getItemCount();
        if(Integer.valueOf(mData.getCount())>mWalletListAdp.getItemCount()){
            mRefreshLayout.setEnableLoadMore(true);
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
        if(listListener != null){
            listListener.onTotalMoney(mData.getData().getTotal_fee());
        }
    }

    private void loadData(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(getArguments() != null){
                    typid = getArguments().getString(WALLETLISTTYPEID,"-1");
                }
                Log.d("dwqdqwddwq","type:"+typid);
                if(mWalletListAdp != null){
                    mWalletListAdp.setType(typid);
                }
                if(mRefreshLayout != null) {
                    mRefreshLayout.autoRefresh();
                }
            }
        },500);
    }

    @Override
    public void setPresenter(WalletListContract.Presenter presenter) {

    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToken() {
        TokenDialog.getInsence(getContext()).show();
    }

    private WalletListListener listListener;

    public void setWalletListListener(WalletListListener listListener){
        this.listListener = listListener;
    }

    public interface WalletListListener{
        void onTotalMoney(String totalMoney);
    }
}
